{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "solar-edinburgh", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 12, -3, 3.4, 3.141592653589793, 19, -12]\n" ] } ], "source": [ "# Lists\n", "\n", "# Lists are created using square brackets.\n", "import numpy as np\n", "xData = [1, 2, 5, 12, -3, 3.4, np.pi, 19, -12]\n", "print(xData)" ] }, { "cell_type": "code", "execution_count": 3, "id": "homeless-stanford", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can use 'len' to check the number of elements in a list.\n", "len(xData)" ] }, { "cell_type": "code", "execution_count": 4, "id": "accompanied-edmonton", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's another list of the same length.\n", "yData = [2, 3, 4, 5, 5, 4, 3, 2, 4]\n", "len(yData)" ] }, { "cell_type": "code", "execution_count": 5, "id": "norman-fireplace", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]\n" ] } ], "source": [ "# You can also automatically generate a list.\n", "x = list(range(11, 22))\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 6, "id": "competitive-insulation", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[11, 13, 15, 17, 19, 21]\n", "[22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12]\n" ] } ], "source": [ "# Using a third option in range, you can specify the increment of the list.\n", "x = list(range(11, 22, 2))\n", "print(x)\n", "y = list(range(22, 11,-1))\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 8, "id": "federal-philippines", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] }, { "data": { "text/plain": [ "-12" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# It is possible select specific elements of a list using square brackets.\n", "# Note that the first element is indexed as zero and the nth element is indexed\n", "# as n-1.\n", "print(xData[0])\n", "xData[len(xData) - 1]" ] }, { "cell_type": "code", "execution_count": 9, "id": "intensive-aspect", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 5, 12, -3, 3.4]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# You can also extract a range of values from the list.\n", "xData[1:6]" ] }, { "cell_type": "code", "execution_count": 12, "id": "naval-capital", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1. 2. 5. 12. -3.\n", " 3.4 3.14159265 19. -12. ]\n" ] } ], "source": [ "# The 'NumPy' module can be used to do algebraic operations on lists and to convert the lists to arrays.\n", "xArray = np.array(xData)\n", "print(xArray)" ] }, { "cell_type": "code", "execution_count": 13, "id": "educated-raise", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2. , 4. , 10. , 24. ,\n", " -6. , 6.8 , 6.28318531, 38. ,\n", " -24. ])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Now you can scale the array...\n", "2*xArray" ] }, { "cell_type": "code", "execution_count": 14, "id": "married-agency", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 4. , 25. , 144. , 9. ,\n", " 11.56 , 9.8696044, 361. , 144. ])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# square each element in the array...\n", "xArray**2" ] }, { "cell_type": "code", "execution_count": 15, "id": "northern-clerk", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ -1. -1. 1. 7. -8.\n", " -0.6 0.14159265 17. -16. ]\n" ] }, { "data": { "text/plain": [ "array([ 2. , 6. , 20. , 60. ,\n", " -15. , 13.6 , 9.42477796, 38. ,\n", " -48. ])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# do element by element addition and products...\n", "yArray = np.array(yData)\n", "print(xArray - yArray)\n", "xArray*yArray" ] }, { "cell_type": "code", "execution_count": 16, "id": "retained-database", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "86.02477796076937" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# evaluate dot products...\n", "np.dot(xArray, yArray)" ] }, { "cell_type": "code", "execution_count": 17, "id": "functioning-optimum", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-7, 14, -7])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# and evaluate cross products of 3-elemnet arrays (among other things)\n", "x3 = np.array([1, 2, 3])\n", "y3 = np.array([6, 5, 4])\n", "np.cross(x3, y3)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }